package com.example.sports_meet;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Objects;

import util.Utils;

public class SettingActivity extends AppCompatActivity {

    private EditText oldPasswordEditText, newPasswordEditText, confirmPasswordEditText;
    private Button exit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        Objects.requireNonNull(getSupportActionBar()).hide();
        Button exitButton = findViewById(R.id.btn_exit);
        oldPasswordEditText = findViewById(R.id.old_password_edittext);
        newPasswordEditText = findViewById(R.id.new_password_edittext);
        confirmPasswordEditText = findViewById(R.id.confirm_password_edittext);
        Button confirmButton = findViewById(R.id.confirm_button);
        exitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Utils.clearLoginInfo(SettingActivity.this);
            }
        });
        confirmButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取旧密码、新密码和确认密码
                String oldPassword = oldPasswordEditText.getText().toString();
                String newPassword = newPasswordEditText.getText().toString();
                String confirmPassword = confirmPasswordEditText.getText().toString();

                // 检查新密码和确认密码是否匹配
                if (!newPassword.equals(confirmPassword)) {
                    Toast.makeText(SettingActivity.this, "新密码和确认密码不匹配", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 检查旧密码是否正确
                if (!checkOldPassword(oldPassword)) {
                    Toast.makeText(SettingActivity.this, "旧密码不正确", Toast.LENGTH_SHORT).show();
                    return;
                }

                // 修改密码
                if (changePassword(newPassword)) {
                    Toast.makeText(SettingActivity.this, "密码修改成功", Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    Toast.makeText(SettingActivity.this, "密码修改失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private boolean checkOldPassword(String oldPassword) {
        SharedPreferences spf = getSharedPreferences("spfRecord", Context.MODE_PRIVATE);
        String password = spf.getString("password", "");

        // 检查用户输入的旧密码是否正确
        if (!password.equals(oldPassword)) {
            return false;
        }


        // TODO: 检查旧密码是否正确，这里需要自己实现
        return true;
    }

    private boolean changePassword(String newPassword) {
        // TODO: 修改密码，这里需要自己实现
        SharedPreferences spf = getSharedPreferences("spfRecord", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = spf.edit();
        editor.putString("password", newPassword);
        editor.commit();
        return true;
    }


}
